<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
  mixins: [
    Vue.prototype.$vFN.Mixins.Base,
    Vue.prototype.$vFN.Mixins.Edit,
    Vue.prototype.$vFN.Mixins.List,
  ],

  data() {
    return {
      appWindow: window,
      value: "", //已选择的权益名称

      powerOptions: [], //权益列表

      apiUrl: {
        powerListUrl: "mall-site-portal/member-rights/list",
        list: "mall-site-portal/member-rights/listRule",
      },

      // 请求携带参数
      mixinList_tableSearchConst: {
        rightId: "",
      },
    };
  },
  methods: {
    init() {
      console.log(this.ruleInfoList); //从edit 传来
      this.getPowerList();

      this.mixinEdit_editVisible = true;
    },

    // 获取权益列表
    async getPowerList() {
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.powerListUrl,
        },
        window
      );

      if (res._ok) {
        this.powerOptions = res.data.list;

        // 默认请求第一个权益对应规则
        this.value = this.powerOptions[0].rightName;

        this.mixinList_tableSearchConst.rightId = this.powerOptions[0].id;

        // 请求第一页
        this.mixinList_getTable();
      }
    },

    // 选择不同权益  发送不同请求
    selectPower(val) {
      console.log(val); //权益的id
      this.mixinList_loading = true;

      this.mixinList_tableSearchConst.rightId = val;
      this.mixinList_getTable();
    },

    btnClick(rowObj) {
      console.error("rowObj", rowObj);
      console.log(this.mixinList_tableData);

      let index_ = this.getRightIdIndex();

      // 如果是大于等于0 则代表这个权益id已经选择过
      if (index_ >= 0) {
        if (!this.isSelected(rowObj.row.id)) {
          // 先把原来选择的删去，再添加新的
          // this.ruleInfoList.splice(index_, 1);
          this.ruleInfoList.push({
            rightId: this.mixinList_tableSearchConst.rightId,
            id: rowObj.row.id,
            rightRuleName: rowObj.row.rightRuleName,
          });
        } else {
          this.ruleInfoList.splice(index_, 1);
          console.log("删", this.ruleInfoList);
        }
      } else {
        this.ruleInfoList.push({
          rightId: this.mixinList_tableSearchConst.rightId,
          id: rowObj.row.id,
          rightRuleName: rowObj.row.rightRuleName,
        });
      }
      console.error("this.ruleInfoList", this.ruleInfoList);

      this.onSuccess(this.ruleInfoList);
      this.mixinEdit_editVisible = false;
    },

    // 成功回调
    onSuccess() {},

    // 在ruleInfoList数组中拿到这个RightId 的 index
    getRightIdIndex() {
      const { rightId } = this.mixinList_tableSearchConst;
      for (let i = 0; i < this.ruleInfoList.length; i++) {
        if (this.ruleInfoList[i].rightId == rightId) {
          return i;
        }
      }
    },

    // 判断是否选择
    isSelected(id) {
      let _list = {};
      this.ruleInfoList.forEach((item) => {
        _list["key" + item.id] = true;
      });

      // console.error(id, _list["key" + id], _list, this.ruleInfoList);
      return _list["key" + id];
    },
  },
  mounted() {
    this.init();
  },
  filters: {},
};
</script>
